// approval/pages/person_apply/person_apply.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgApproval: '',
    stautsApproval: true,
    readDataShow: false,
    nameFile: '',
    showType: false,
    columns: [{
        index: 0,
        name: '员工'
      },
      {
        index: 0,
        name: '组长'
      },
      {
        index: 0,
        name: '部门负责人'
      },
      {
        index: 0,
        name: '经理'
      }
    ],
    addStaffExam: {
      staffJob: '',
      staffNum: '',
      postRank: '',
      postSetNum: '',
      postNowNum: '',
      staffReason: '',
      arriveDate: '',
      staffDuty: '',
      staffQualification: '',
      staffSpecial: '',
      staffSalary: '',
      staffStatus: 0,
    },
    examInfo: {
      approval: false,
      examMemberDetail: '',
      detial: true,
      return: false,
    },
    listProps: {
      topList: [],
      btmList: [],
      readonlyStatus: false,
    },
    isMusk: false,
    showTime: false,
    currentDate: new Date().getTime(), //选择时间
    showTime: false, //选择时间弹窗
    value: '',
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    },
  },
  //判断时间遮罩标题
  timeData(e) {
    this.setData({
      showTime: true,
      currentDate: new Date().getTime()
    })
  },
  // 选择时间
  resTime(e) {
    var time = new Date(e.detail),
      str = 'addStaffExam.arriveDate',
      formartTime = this.formatDate(time);
    this.setData({
      [str]: formartTime,
      showTime: false
    })
  },
  formatDate(date) {
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  // 取消选择时间 
  selectCancel() {
    this.setData({
      showTime: false
    })
  },
  // 组件事件
  change(obj) {
    console.log(obj);
    this.setData({
      listProps: JSON.parse(wx.getStorageSync('listProps'))
    })
    console.log(this.data.listProps);
  },
  // 输入框绑定
  inputHandle(e) {
    console.log(e);
    var val = e.detail,
      str = e.currentTarget.dataset.str;
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },
  // 选择岗位职级
  onSelectType(e) {
    console.log(e);
    var val = e.detail.name,
      index = e.detail.index,
      str = 'addStaffExam.postRank';
    console.log(val);

    this.setData({
      [str]: index,
      nameFile: val,
      showType: false
    })
  },
  onTypeClose() {
    this.setData({
      showType: false
    })
  },
  showType() {
    this.setData({
      showType: true
    })
  },
  getExamProcessDetail() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/getExamProcessDetail",
      that = this,
      listProps = this.data.listProps;
    app.wxRequest(url, {
      examType: 6,
      memberId: app.globalData.memberId
    }, res => {
      console.log(res);
      if (res.flag) {
        listProps.topList = res.data['审批人'];
        listProps.btmList = res.data['抄送人'];
        console.log(listProps);
        that.setData({
          listProps,
        })
        wx.hideLoading()
      }
    })

  },
  // 获取 详情
  resultData() {
    var examInfo = JSON.parse(wx.getStorageSync('examInfo')),
      listProps = this.data.listProps;
    listProps.readonlyStatus = true;
    this.setData({
      examInfo,
      listProps
    })
    console.log(this.data.examInfo);
    this.getExamDetail();
    if (this.data.examInfo.examMember_status == 4) {
      this.setData({
        readDataShow: true
      })
    }
    // if (wx.getStorageSync('readDataOa') != '') {
    //   this.setData({
    //     readDataShow: true
    //   })
    // }
    if (examInfo.stautsApproval != undefined) {
      this.setData({
        stautsApproval: false
      })
    }
    if (!examInfo.detial) {
      this.getPenSigner()
    }
    wx.removeStorageSync('examInfo')
    wx.removeStorageSync('readDataOa')
  },
  // 获取签名版
  getPenSigner() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    console.log(this.data.examInfo);
    var url = app.globalData.url + 'member/getPenSigner',
      that = this;
    app.wxRequest(url, {
      memberId: app.globalData.memberId
    }, res => {
      if (res.flag) {
        if (res.data.penSigners.length == 0) {
          that.setData({
            imgApproval: ''
          })
        } else {
          that.setData({
            imgApproval: res.data.penSigners[0].penSignerImg
          })
        }
      }
    })
  },
  getExamDetail() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/getExamDetail",
      that = this;
    app.wxRequest(url, {
      examId: that.data.examInfo.examMain_id
    }, res => {
      if (res.flag) {
        var listProps = that.data.listProps,
          addStaffExam = {},
          nameFile = '',
          columns = that.data.columns;
        addStaffExam = res.data.staffExam;
        addStaffExam.memberName = res.data.examMain.member_name;
        for (var i in columns) {
          if (addStaffExam.postRank == i) {
            nameFile = columns[i].name
          }
        }
        for (var i in res.data.examMembers) {
          res.data.examMembers[i].memberName = res.data.examMembers[i].member_name;
          res.data.examMembers[i].memberImg = res.data.examMembers[i].member_img;
          if (res.data.examMembers[i].examMember_type == 0) {
            listProps.topList.push(res.data.examMembers[i])
          } else {
            listProps.btmList.push(res.data.examMembers[i])
          }
        }
        that.setData({
          listProps,
          addStaffExam,
          nameFile
        })
        wx.hideLoading()
      }
    })
  },
  // 同意 或 不同意
  approvalData(e) {
    var flag = e.currentTarget.dataset.flag;
    if (!this.data.examInfo.detial && this.data.imgApproval.length == 0) {
      wx.showModal({
        title: '提示',
        content: '暂无签名板,请完善签名板!',
        complete: (res) => {
          if (res.confirm) {
            console.log('签名版');
            wx.navigateTo({
              url: '/gerenInfo/pages/signature/signature',
            })
          }
        }
      })
      return
    }
    var examMemberNum = '',
      listProps = this.data.listProps,
      url = app.globalData.url + 'exam/reviewExam',
      that = this;
    console.log(listProps);
    for (let i in listProps.topList) {
      if (listProps.topList[i].member_id == wx.getStorageSync('member_id')) {
        examMemberNum = listProps.topList[i].examMember_num;
      }
    }
    console.log(examMemberNum);
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    app.wxRequest(url, {
      examMemberId: that.data.examInfo.examMember_id,
      examMemberNum: examMemberNum,
      examMemberDetail: that.data.examInfo.examMemberDetail == undefined ? '' : that.data.examInfo.examMemberDetail,
      examMemberStatus: flag,
      examType: 6,
      note: that.data.imgApproval,
      examMainId: that.data.examInfo.examMain_id,
      examMemberType: that.data.examInfo.detial ? 1 : 0
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '审核成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  // 已阅
  readData() {
    var examMemberNum = '',
      examMemberType = "",
      listProps = this.data.listProps,
      that = this,
      url = app.globalData.url + 'exam/reviewExam';
    for (let i in listProps.btmList) {
      if (listProps.btmList[i].member_id == wx.getStorageSync('member_id')) {
        examMemberNum = listProps.btmList[i].examMember_num;
        examMemberType = listProps.btmList[i].examMember_type;
      }
    }
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    app.wxRequest(url, {
      examMemberId: that.data.examInfo.examMember_id,
      examMemberNum: examMemberNum,
      examMemberDetail: that.data.examInfo.examMemberDetail == undefined ? '' : that.data.examInfo.examMemberDetail,
      examMemberStatus: 1,
      examType: 6,
      examMainId: that.data.examInfo.examMain_id,
      examMemberType: examMemberType
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '审核成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },

  // 确认提交
  toSubmit() {
    var examMemberIdList = [],
      leaveExamTypeList = [],
      listProps = this.data.listProps,
      addStaffExam = this.data.addStaffExam;
    for (let i in listProps.topList) {
      examMemberIdList.push(listProps.topList[i].memberId);
      leaveExamTypeList.push(0)
    }
    for (let i in listProps.btmList) {
      examMemberIdList.push(listProps.btmList[i].memberId);
      leaveExamTypeList.push(1)
    }

    if (addStaffExam.staffJob.length == 0) {
      wx.showToast({
        title: '请输入岗位需求',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.staffNum.length == 0) {
      wx.showToast({
        title: '请输入岗位需求人数',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.postRank.length == 0) {
      wx.showToast({
        title: '请选择岗位职级',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.postSetNum.length == 0) {
      wx.showToast({
        title: '请输入岗位定编人数',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.postNowNum.length == 0) {
      wx.showToast({
        title: '请输入岗位现有人数',
        icon: 'none'
      })
      return
    }

    if (addStaffExam.staffReason.length == 0) {
      wx.showToast({
        title: '请输入岗位需求原因',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.arriveDate.length == 0) {
      wx.showToast({
        title: '请输入到岗日期',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.staffDuty.length == 0) {
      wx.showToast({
        title: '请输入岗位职责',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.staffQualification.length == 0) {
      wx.showToast({
        title: '请输入岗位需求品质',
        icon: 'none'
      })
      return
    }
    if (addStaffExam.staffSalary.length == 0) {
      wx.showToast({
        title: '请输入薪资待遇',
        icon: 'none'
      })
      return
    }
    if (examMemberIdList.length == 0) {
      wx.showToast({
        title: '审批人为空',
        icon: 'none'
      })
      return
    }
    if (leaveExamTypeList.length == 0) {
      wx.showToast({
        title: '抄送人为空',
        icon: 'none'
      })
      return
    }

    console.log(addStaffExam);
    console.log(examMemberIdList);
    console.log(leaveExamTypeList);


    var url = app.globalData.url + "exam/addStaffExam",
      that = this;
    app.wxRequest(url, {
      staffJob: addStaffExam.staffJob,
      staffNum: addStaffExam.staffNum,
      postRank: addStaffExam.postRank,
      postSetNum: addStaffExam.postSetNum,
      postNowNum: addStaffExam.postNowNum,
      staffReason: addStaffExam.staffReason,
      arriveDate: addStaffExam.arriveDate,
      staffDuty: addStaffExam.staffDuty,
      staffQualification: addStaffExam.staffQualification,
      staffSpecial: addStaffExam.staffSpecial,
      staffSalary: addStaffExam.staffSalary,
      staffStatus: 0,
      examType: 6,
      memberId: app.globalData.memberId,
      examMemberId: examMemberIdList.toString(),
      examLeaveExamType: leaveExamTypeList.toString(),
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
          iocn: 'none'
        })
        var obj = {
          examMain_id: res.data,
          return: true,
          detial: true,
          stautsApproval: true
        }
        listProps = {
            topList: [],
            btmList: [],
            readonlyStatus: false, // 是否只读
          },
          wx.setStorageSync('examInfo', JSON.stringify(obj))
        that.setData({
          listProps
        })
        that.resultData();
      }
    })

  }, // 撤回
  revokeExam() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    console.log(this.data.examInfo);
    var url = app.globalData.url + 'exam/revokeExam',
      that = this;
    app.wxRequest(url, {
      examMainId: that.data.examInfo.examMain_id
    }, res => {
      if (res.flag) {
        console.log(res);
        wx.showModal({
          title: '提示',
          content: '撤回审批成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
              wx.removeStorageSync('examInfo')
            }
          }
        })
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('examInfo') != '') {
      this.resultData()
    } else {
      this.getExamProcessDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getPenSigner()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})